import { Effect, ImmerReducer, Reducer, Subscription } from 'umi';
import { message } from 'antd'
import { findAllMenu, updateMenu, searchMenu } from '../api/productReq'
import storageUtils from '../utils/storageUtils'
import { history } from 'umi'



interface ModelState {
    productData: listType[];
}
type listType = {
	_id: string,
	classtips: string[],
	collections: number,
	coverpic: string,
	grade: string,
	ingredient: object[],
    isFree:string,
    isHot:string,
    isRecommend:string,
    method:object[],
    name:string,
    needtime:string,
    pageview:number,
    state:string,
    tips:object[],
    vid:string,
}
interface ModelsType {
    state: ModelState;
    effects: {
        findAllMenu: Effect;
        updateMenu: Effect;
        searchMenu: Effect;
    };
    reducers: {
        initMenu: ImmerReducer
    }

}

const productModel: ModelsType = {

    state: {
        productData: [],
    },
    effects: {
        *findAllMenu({ payload }, { call, put }) {
            const res = yield call(findAllMenu)
            console.log(res);//这是获取到的结果
            if(res.meta.status==200){
                yield put({
                    type: 'initMenu',
                    payload:res.menus
                })
            }
        },
        *updateMenu({ payload }, { call, put }) {
            const res = yield call(() => updateMenu(payload))
            console.log(res);//这是获取到的结果
        },
        *searchMenu({ payload }, { call, put }) {
            const res = yield call(() => searchMenu(payload))
            console.log(res);//这是获取到的结果
        },

    },
    reducers: {
        initMenu(state, action) {
            // console.log(state, action);
            state.productData = action.payload
        },
    },

};

export default productModel;
